<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>

<%@ taglib uri="/WEB-INF/hexacta.tld" prefix="hexacta"%>

<head>
<sx:head cache="true" />
<link rel="stylesheet" href="/hrs-intranet/styles/displaytag.css"
	type="text/css" />
</head>

<s:include value="errorsSection.jsp"></s:include>

<link type="text/css" href="/hrs-intranet/js/jquery/css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />	
<script type="text/javascript" src="/hrs-intranet/js/jquery/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="/hrs-intranet/js/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
	
<script type="text/javascript" src="/hrs-intranet/js/cluetip/jquery.cluetip.js"></script>
<link type="text/css" href="/hrs-intranet/js/cluetip/jquery.cluetip.css" rel="stylesheet" />
	
	
<script type="text/javascript">

	function setEdit(assigId,employeeId,employeeName,roleCode, roleDescription,assignmentPorcentage,comments,addOnAvailable,addOnNotAvailable,tentative, startDate, endDate, projectId, projectName){
		
		document.getElementById('assignmentIdBefore_edit').value=assigId;
		document.getElementById('selectedGanttEmployeeIdBefore_edit').value=employeeId;
		document.getElementById('selectedGanttEmployeeNameBefore_edit').value=employeeName;
		document.getElementById('employee_edit').value=employeeName;
		var rolesAutoCompleter = dojo.widget.byId("assigRoles_edit");
		rolesAutoCompleter.setAllValues(roleDescription, roleCode);
		document.getElementById('selectedRoleCode').value=roleCode;
		var startDatePicker= dojo.widget.byId("startDate_edit");
		startDatePicker.setValue(new Date(startDate));
		var endDatePicker= dojo.widget.byId("endDate_edit");
		endDatePicker.setValue(new Date(endDate));
		document.getElementById('assigPercentage_edit').value=assignmentPorcentage;
		document.getElementById('assigDescription_edit').value=comments;
		document.getElementById('addonAvailable_edit').checked=addOnAvailable;
		document.getElementById('addonNotAvailable_edit').checked=addOnNotAvailable;
		document.getElementById('assignTentative_edit').checked=tentative;
		document.getElementById('project_id').value=projectId;
		document.getElementById('project_name').value=projectName;
		document.getElementById('project_selected').value=projectName;
	} 
	
	function confirmEdit(assignmenId){
		document.getElementById('selectedAssignmentId').value = assignmenId;
		document.getElementById('fromPopUp').value = true;
		document.forms[0].action = 'EmployeeAssignmentAction_editAssignmentFromPopUp';
  		document.forms[0].submit();
	return true;
	}
	
	function confirmDeleteAssign(url, id) {
		if (confirm("Are you sure you want to delete this assignment?")) {
			document.getElementById('assignmentToDelete').value = id;
			document.forms[0].action = url;
			document.forms[0].submit();
		}
	}

	function createNewAssignment(url) {
		document.forms[0].action = url;
		document.forms[0].submit();
	}

	function destildarAvailable() {
		document.getElementById('addonAvailable').checked = "";
		destildarRequerimiento();

	}

	function destildarNotAvailable() {
		document.getElementById('addonNotAvailable').checked = "";
		destildarRequerimiento();
	}
	
	function destildarAvailableEdit() {
		document.getElementById('addonAvailable_edit').checked = "";
		destildarRequerimiento();

	}

	function destildarNotAvailableEdit() {
		document.getElementById('addonNotAvailable_edit').checked = "";
		destildarRequerimiento();
	}

	
	function destildarRequerimiento() {
		if (document.getElementById('addonAvailable').checked
				|| document.getElementById('addonNotAvailable').checked) {
			document.getElementById('createRequirement').checked = false;
			document.getElementById('createRequirement').disabled = true;
		} else {
			document.getElementById('createRequirement').checked = true;
			document.getElementById('createRequirement').disabled = false;
		}
	}

	$('textarea[maxlength]').live('keyup blur', function() {
		// Store the maxlength and value of the field.
		var maxlength = $(this).attr('maxlength');
		var val = $(this).val();

		// Trim the field if it has content over the maxlength.
		if (val.length > maxlength) {
			$(this).val(val.slice(0, maxlength));
		}
	});
</script>

<script type="text/javascript">
	function showDialog(id) {
		$(id).dialog('open');
		return false;
	}
	
	function showDialog(id, employeeId, employeeName, projectId, projectName) {
		var employeeAutoCompleter = dojo.widget.byId("assigEmployeeList_new");
		employeeAutoCompleter.setAllValues(employeeName, employeeId);
		var projectAutoCompleter = dojo.widget.byId("projectsList_new");
		projectAutoCompleter.setAllValues(projectName, projectId);
		projectAutoCompleter.setSelectedKey(projectId);
		$(id).dialog('open');
		return false;
	}
	
	$(document).ready(function() {

		// Dialog			
		$("div[id^='dialog']").dialog({
			autoOpen : false,
			modal : true,
			draggable : false,
			resizable : false,
			width : 500,
			height : 'auto',
			zIndex:0,
			hide: { effect: 'drop', direction: "down" },
			show: { effect: 'drop', direction: "up" }
		});

		// 				$("div[id^='dialog']").dialog('widget').find(
		// 						".ui-dialog-titlebar").hide();

		$(".ui-dialog-titlebar").hide();
		
		
	});
	

</script>

<s:url var="edit_action"
	action="EmployeeAssignmentAction_editAssignment" namespace="/hrs2" />
<s:url var="delete_action"
	action="EmployeeAssignmentAction_deleteAssignment" namespace="/hrs2" />
<s:url var="create_action"
	action="EmployeeAssignmentAction_createNewAssignment" namespace="/hrs2" />
<s:url var="return_to_search_action" action="%{returnToSearchAction}"
	namespace="/hrs2" />

<%-- <s:if
	test="%{(assignmentIdBefore != null)&&(assignmentIdBefore != '') && hasPermission(permissionNameConverter.getEditProjectAssignmentPermission())}">
	<h2 style="position: relative; top: -7px;">Edit assignment</h2>
</s:if>
<s:elseif
	test="hasPermission(permissionNameConverter.getCreateProjectAssignmentPermission())">
	<h2 style="position: relative; top: -7px;">Create new assignment</h2>
</s:elseif> --%>


<s:form cssClass="formClass">
	<table style="position: relative;">
		<tr>
			<td><s:if
					test="hasPermission(permissionNameConverter.getCreateProjectAssignmentPermission())">
					<s:hidden name="selectedGanttEmployeeNameBefore"
						id="selectedGanttEmployeeNameBefore" />
					<s:hidden name="employeeId" id="employeeId" />
					<s:hidden name="projectId" id="projectId" />
					<s:hidden name="roleId" id="roleId" />
					<s:hidden name="assignmentToDelete" id="assignmentToDelete" />
					<s:hidden name="assignmentIdBefore" id="assignmentIdBefore" />
					<s:hidden name="selectedGanttEmployeeIdBefore" id="selectedGanttEmployeeIdBefore" />
					<s:hidden name="selectedAssignmentId" id="selectedAssignmentId"></s:hidden>
					<s:hidden name="selectedProjectId" id="selectedProjectId"></s:hidden>
					<s:hidden name="selectedProjectName" id="selectedProjectName"></s:hidden>
					<%-- <s:hidden name="selectedRoleCode" id="selectedRoleCode"></s:hidden>	    
					<s:hidden name="selectedRoleName" id="selectedRoleName"></s:hidden> --%>
					<s:hidden name="fromPopUp" id="fromPopUp"></s:hidden>
				</s:if>


				<table width="67%" style="top: +50px;">
					<tr>
						<td><h2>Employee Assignments</h2>
						</td>
					</tr>
					<tr>
						<td colspan="2"><s:if
								test="%{!assignmentList.isEmpty() || !assignmentList.size()==0}">
								<display:table id="assignmentList" name="assignmentList"
									defaultsort="1" defaultorder="descending"
									requestURI="EmployeeAssignmentAction_createNewAssignment"
									class="listable" cellpadding="0" cellspacing="0" pagesize="10">
									<display:column property="employee.person.name"
										title="Employee" sortable="true" />
									<display:column property="project.name" title="Project"
										sortable="true" />
									<display:column property="availabilityStatus"
										title="Assignment Type" sortable="true" />
									<display:column property="assignedRole.description"
										title="Assignment Role" sortable="true" />

									<display:column property="FTE" title="% FTE"
										sortable="true" format="{0,number,00} %" />

									<display:column property="startDate.time" title="Start Date"
										sortable="true" format="{0,date,dd/MM/yyyy}" />
									<display:column property="endDate.time" title="End Date"
										sortable="true" format="{0,date,dd/MM/yyyy}" />
									<display:column
										value="${(assignmentList.tentative)? 'Yes' : 'No'}"
										title="Tentative" sortable="true" />

									<display:column title="Options">
										<s:if
											test="hasPermission(permissionNameConverter.getEditProjectAssignmentPermission())">
										<s:a href="#" id="dialog_link"
													onclick="javascript: setEdit('%{#attr.assignmentList.id}','%{#attr.assignmentList.employee.id}','%{#attr.assignmentList.employee.person.name}','%{#attr.assignmentList.assignedRole.code}', '%{#attr.assignmentList.assignedRole.description}',%{#attr.assignmentList.assignmentPorcentage},'%{#attr.assignmentList.comments}',%{#attr.assignmentList.addOnAvailable},%{#attr.assignmentList.addOnNotAvailable},%{#attr.assignmentList.tentative}, %{#attr.assignmentList.startDate.timeInMillis}, %{#attr.assignmentList.endDate.timeInMillis}, '%{#attr.assignmentList.project.id}', '%{#attr.assignmentList.project.name}'); return showDialog('#dialogEdit');">Edit</s:a>
									</s:if>
										<s:if
											test="hasPermission(permissionNameConverter.getRemoveProjectAssignmentPermission())">
											<a
												href="javascript:confirmDeleteAssign('${delete_action}', '${assignmentList.id}')">Remove</a>&nbsp;&nbsp;&nbsp;
									</s:if>
									</display:column>
								</display:table>
							</s:if>
						</td>
					</tr>
					<s:if
						test="%{employeeAssignmentList.isEmpty() || employeeAssignmentList.size()==0}">
						<table width="67%">
							<tr>
								<td><s:label label="No current matches found"
										cssClass="formClass"></s:label>
								</td>
							</tr>
						</table>
					</s:if>
					<s:else>
						<tr>
							<td colspan="2"><jsp:include page="availableResources.jsp">
									<jsp:param name="requestURI"
										value="EmployeeAssignmentAction_createNewAssignment" />
									<jsp:param name="addEditURI"
										value="EmployeeAssignmentAction_createNewAssignment" />
									<jsp:param name="list" value="employeeAssignmentList" />
									<jsp:param name="optionToShowInGantt" value="Project" />
									<jsp:param name="showOptionsInGantt" value="0" />
								</jsp:include></td>
						</tr>
					</s:else>
				</table>
		</td>
		<td valign="top" align="left" width="10%;"> 
			<span class="pageLink" style="position: left;">
			 		<s:a href="%{#return_to_search_action}">
						<s:text name=" &lt;&lt;&lt; Return to search" />
					</s:a> <br /> <br /> <s:if
						test="hasPermission(permissionNameConverter.getCreateProjectAssignmentPermission())">
						<s:a cssClass="Add" href="#" id="dialog_link"
							onclick="javascript: return showDialog('#dialogAssig','%{selectedGanttEmployeeIdBefore}', '%{selectedGanttEmployeeNameBefore}', '%{selectedProjectId}', '%{selectedProjectName}');">Create New Assignment</s:a>
					</s:if> </span>
			</td>
		</tr>
	</table>
</s:form>

<div id="dialogAssig" title="Create New Assignment">
	<h2>Create New Assignment</h2>
	<s:if
		test="hasPermission(permissionNameConverter.getCreateProjectAssignmentPermission())">
		<div align="right" style="margin-top: 50px">
			<s:form cssClass="formClass">
				<s:hidden name="employeeId" id="employeeId" />
				<s:hidden name="projectId" id="selectedProjectId" />
				<s:hidden name="selectedRoleCode" id="selectedRoleCode" />
				<sx:autocompleter id="assigEmployeeList_new" list="employeeList"
					listValue="person.name" searchType="startword" listKey="id"
					showDownArrow="false" keyName="selectedGanttEmployeeIdBefore"
					name="selectedGanttEmployeeNameBefore" label="Employee"/>
				<sx:autocompleter id="projectsList_new" list="projectsList"
				listValue="name" searchType="startword" listKey="id"
				showDownArrow="false" keyName="selectedProjectId"
				name="selectedProjectName" label="Project" />
				<sx:autocompleter id="assigRoles" list="roles"
					listValue="description" searchType="startword" listKey="code"
					showDownArrow="false" keyName="selectedRoleCode" 
					name="selectedRoleName" label="Assigned Role"/>
				<s:textfield label="Assignment %" name="selectedAssignmentPercentage"
					value="100" cssClass="dojoComboBox" />
				<sx:datetimepicker id="startDate" name="selectedStartDate"
					label="Start Date" displayFormat="dd/MM/yyyy" />
				<sx:datetimepicker id="endDate" name="selectedEndDate"
					label="End Date" displayFormat="dd/MM/yyyy" />

				<s:textarea key="description" label="Description" rows="4" name="description"
					cols="16" cssClass="formTextAreaClass" wrap="50" maxlength="250" />
				<s:checkbox label="Addon available" name="addonAvailable"
					labelposition="inline" id="addonAvailable"
					onclick="destildarNotAvailable()" />
				<s:checkbox label="Addon not available" name="addonNotAvailable"
					id="addonNotAvailable" onclick="destildarAvailable()" />
				<s:checkbox label="Tentative" name="assignTentative" />
				<s:checkbox label="Create Project Requirement"
					id="createRequirement" name="createRequirement" />
				<s:checkbox name="fromPopUp" value="false" style="visibility:hidden;" />
				<table>
					<tr>
						<td>
							<table>
								<tr>
									<td><s:submit key="Ok" cssClass="btn"
											cssStyle="align:left" id="okbtn"
											action="EmployeeAssignmentAction_confirmNewAssignment" />
									</td>
								</tr>
							</table>
						</td>
						<td>
							<table>
								<tr>
									<td><input type="button" value="Cancel" class="btn"
										onclick="$('#dialogAssig').dialog('close');" />
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</s:form>
		</div>

	</s:if>
	<s:else>
		<br />
		<p>You don't have enough permissions to create assignments.</p>

		<input type="button" value="Close" class="btn"
			style="text-align: center"
			onclick="$('#dialogAssig').dialog('close');" />
	</s:else>
</div>



<div id="dialogEdit" title="Edit Assignment">
	<h2>Edit Assignment</h2>
	<s:if
		test="hasPermission(permissionNameConverter.getCreateProjectAssignmentPermission())">
		<div align="right" style="margin-top: 50px">
			<s:form cssClass="formClass">
				<s:hidden name="assignmentIdBefore" id="assignmentIdBefore_edit" />
				<s:hidden name="selectedGanttEmployeeIdBefore"
					id="selectedGanttEmployeeIdBefore_edit" />
				<s:hidden name="selectedGanttEmployeeNameBefore"
					id="selectedGanttEmployeeNameBefore_edit" />
				<s:hidden name="selectedProjectId"
					id="project_id" />
				<s:hidden name="selectedProjectName"
					id="project_name" />
				<s:hidden name="selectedRoleCode" id="selectedRoleCode" />
				<s:textfield label="Employee" disabled="true"
					cssClass="dojoComboBox" id="employee_edit" />
				<s:textfield label="Project" disabled="true"
					cssClass="dojoComboBox" id="project_selected" />
				<sx:autocompleter id="assigRoles_edit" list="roles" 
					listValue="description" searchType="startword" listKey="code"
					showDownArrow="false" keyName="selectedRoleCode" 
					name="selectedRoleName" label="Assigned Role"/>
				<s:textfield label="Assignment %" name="selectedAssignmentPercentage"
					cssClass="dojoComboBox" id="assigPercentage_edit" />
				<sx:datetimepicker id="startDate_edit" name="selectedStartDate"
					label="Start Date" displayFormat="dd/MM/yyyy" />
				<sx:datetimepicker id="endDate_edit" name="selectedEndDate"
					label="End Date" displayFormat="dd/MM/yyyy" />
				<s:textarea key="description" label="Description" rows="4" name="description"
					cols="16" cssClass="formTextAreaClass" wrap="50" maxlength="250"
					id="assigDescription_edit" />
				<s:checkbox label="Addon available" name="addonAvailable"
					labelposition="inline" id="addonAvailable_edit"
					onclick="destildarNotAvailableEdit()" />
				<s:checkbox label="Addon not available" name="addonNotAvailable"
					id="addonNotAvailable_edit" onclick="destildarAvailableEdit()" />
				<s:checkbox label="Tentative" name="assignTentative"
					id="assignTentative_edit" />
				<s:checkbox name="fromPopUp" value="false" style="visibility:hidden;" />
				<table>
					<tr>
						<td>
							<table>
								<tr>
									<td><s:submit key="Ok" cssClass="btn" id="okbtn"
											action="EmployeeAssignmentAction_confirmNewAssignment" /> <!-- 										<input type="button" value="Ok" class="btn" -->
										<!-- 										onclick="llamarAssig()" /> -->
									</td>
								</tr>
							</table></td>

						<td>
							<table>

								<tr>
									<td><input type="button" value="Cancel" class="btn"
										onclick="$('#dialogEdit').dialog('close');" />
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</s:form>
		</div>
	</s:if>
	<s:else>
		<br />
		<p>You don't have enough permissions to edit assignments.</p>

		<input type="button" value="Close" class="btn"
			style="text-align: center"
			onclick="$('#dialogEdit').dialog('close');" />
	</s:else>
</div>
